<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Mocks  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
    <script src="js/lunr.min.js" defer></script>
    <script src="js/typeahead.jquery.js" defer></script>
    <script src="js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a title="Mocks  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">Sourcery 2.3.0 Docs</a> (100% documented)</p>
        <p class="header-right"><a href="https://github.com/krzysztofzablocki/Sourcery"><img src="img/gh.png"/>View on GitHub</a></p>
        <p class="header-right">
          <form role="search" action="search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">Sourcery Reference</a>
        <img id="carat" src="img/carat.png" />
        Mocks  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Guides.html">Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="installing.html">Installing</a>
              </li>
              <li class="nav-group-task">
                <a href="usage.html">Usage</a>
              </li>
              <li class="nav-group-task">
                <a href="writing-templates.html">Writing templates</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Examples.html">Examples</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="equatable.html">Equatable</a>
              </li>
              <li class="nav-group-task">
                <a href="hashable.html">Hashable</a>
              </li>
              <li class="nav-group-task">
                <a href="enum-cases.html">Enum cases</a>
              </li>
              <li class="nav-group-task">
                <a href="lenses.html">Lenses</a>
              </li>
              <li class="nav-group-task">
                <a href="mocks.html">Mocks</a>
              </li>
              <li class="nav-group-task">
                <a href="codable.html">Codable</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Diffable.html">Diffable</a>
              </li>
              <li class="nav-group-task">
                <a href="diffable.html">Diffable</a>
              </li>
              <li class="nav-group-task">
                <a href="linuxmain.html">LinuxMain</a>
              </li>
              <li class="nav-group-task">
                <a href="decorator.html">Decorator</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Types.html">Types</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/Types.html">Types</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Type.html">Type</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Protocol.html">Protocol</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Class.html">Class</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Struct.html">Struct</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Enum.html">Enum</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/EnumCase.html">EnumCase</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/AssociatedValue.html">AssociatedValue</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/AssociatedType.html">AssociatedType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Variable.html">Variable</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Method.html">Method</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/MethodParameter.html">MethodParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Subscript.html">Subscript</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/TypeName.html">TypeName</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/TupleType.html">TupleType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/TupleElement.html">TupleElement</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ArrayType.html">ArrayType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/DictionaryType.html">DictionaryType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ClosureType.html">ClosureType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericType.html">GenericType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericTypeParameter.html">GenericTypeParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Attribute.html">Attribute</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ProtocolComposition.html">ProtocolComposition</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Classes.html">Other Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/Actor.html">Actor</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/ClosureParameter.html">ClosureParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/DiffableResult.html">DiffableResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericParameter.html">GenericParameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericRequirement.html">GenericRequirement</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/GenericRequirement/Relationship.html">– Relationship</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Import.html">Import</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Modifier.html">Modifier</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/SetType.html">SetType</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Enums.html">Other Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/Composer.html">Composer</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Extensions.html">Other Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/Array.html">Array</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Extensions.html#/c:@M@SourceryRuntime@objc(cs)BytesRange">BytesRange</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Extensions.html#/c:@M@SourceryRuntime@objc(cs)FileParserResult">FileParserResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/String.html">String</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/StringProtocol.html">StringProtocol</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Typealias.html">Typealias</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Protocols.html">Other Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/Annotated.html">Annotated</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Definition.html">Definition</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Documented.html">Documented</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Typed.html">Typed</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Typealiases.html">Other Type Aliases</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Other%20Typealiases.html#/s:15SourceryRuntime11Annotationsa">Annotations</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Typealiases.html#/s:15SourceryRuntime13Documentationa">Documentation</a>
              </li>
              <li class="nav-group-task">
                <a href="Other%20Typealiases.html#/s:15SourceryRuntime0A8Modifiera">SourceryModifier</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            
            <h2 id='i-want-to-generate-test-mocks-for-protocols' class='heading'>I want to generate test mocks for protocols</h2>

<p><em>Contributed by <a href="http://twitter.com/marinbenc">@marinbenc</a></em></p>
<h4 id='for-each-protocol-implementing-code-automockable-code-protocol-or-a-href-writing-20templates-md-using-source-annotations-annotated-a-with-code-automockable-code-annotation-it-will' class='heading'>For each protocol implementing <code>AutoMockable</code> protocol or <a href="Writing%20templates.md#using-source-annotations">annotated</a> with <code>AutoMockable</code> annotation it will&hellip;</h4>

<p>Create a class called <code>ProtocolNameMock</code> in which it will&hellip;</p>

<p><strong>For each function:</strong></p>

<ul>
<li>Implement the function</li>
<li>Add a <code>functionCalled</code> boolean to check if the function was called</li>
<li>Add a <code>functionReceivedArguments</code> tuple to check the arguments that were passed to the function</li>
<li>Add a <code>functionReturnValue</code> variable and return it when the function is called.</li>
</ul>

<p><strong>For each variable:</strong></p>

<ul>
<li>Add a gettable and settable variable with the same name and type</li>
</ul>
<h4 id='issues-and-limitations' class='heading'>Issues and limitations:</h4>

<ul>
<li>Overloaded methods will produce compiler errors since the variables above the functions have the same name. Workaround: delete the variables on top of one of the functions, or rename them.</li>
<li>Handling success/failure cases (for callbacks) is tricky to do automatically, so you have to do that yourself.</li>
<li>This is <strong>not</strong> a full replacement for hand-written mocks, but it will get you 90% of the way there. Any more complex logic than changing return types, you will have to implement yourself. This only removes the most boring boilerplate you have to write.</li>
</ul>
<h3 id='a-href-https-github-com-krzysztofzablocki-sourcery-blob-master-templates-templates-automockable-stencil-stencil-template-a' class='heading'><a href="https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/Templates/AutoMockable.stencil">Stencil template</a></h3>
<h4 id='example-output' class='heading'>Example output:</h4>
<pre class="highlight swift"><code><span class="kd">class</span> <span class="kt">MockableServiceMock</span><span class="p">:</span> <span class="kt">MockableService</span> <span class="p">{</span>
    <span class="c1">//MARK: - functionWithArguments</span>
    <span class="k">var</span> <span class="nv">functionWithArgumentsCalled</span> <span class="o">=</span> <span class="kc">false</span>
    <span class="k">var</span> <span class="nv">functionWithArgumentsReceivedArguments</span><span class="p">:</span> <span class="p">(</span><span class="nv">firstArgument</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">onComplete</span><span class="p">:</span> <span class="p">(</span><span class="kt">String</span><span class="p">)</span><span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)?</span>

    <span class="c1">//MARK: - functionWithCallback</span>
    <span class="k">var</span> <span class="nv">functionWithCallbackCalled</span> <span class="o">=</span> <span class="kc">false</span>
    <span class="k">var</span> <span class="nv">functionWithCallbackReceivedArguments</span><span class="p">:</span> <span class="p">(</span><span class="nv">firstArgument</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">onComplete</span><span class="p">:</span> <span class="p">(</span><span class="kt">String</span><span class="p">)</span><span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)?</span>

    <span class="kd">func</span> <span class="nf">functionWithCallback</span><span class="p">(</span><span class="n">_</span> <span class="nv">firstArgument</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">onComplete</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">String</span><span class="p">)</span><span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">functionWithCallbackCalled</span> <span class="o">=</span> <span class="kc">true</span>
        <span class="n">functionWithCallbackReceivedArguments</span> <span class="o">=</span> <span class="p">(</span><span class="nv">firstArgument</span><span class="p">:</span> <span class="n">firstArgument</span><span class="p">,</span> <span class="nv">onComplete</span><span class="p">:</span> <span class="n">onComplete</span><span class="p">)</span>
    <span class="p">}</span>
  <span class="o">...</span>
</code></pre>

          </section>
        </section>
        <section id="footer">
          <p>Copyright © 2016-2021 Pixle. All rights reserved.</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="noopener" rel="external">jazzy ♪♫ v0.14.0</a>, a <a class="link" href="https://realm.io" target="_blank" rel="noopener" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</html>
